<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Audio extends Controller_Index_Base
{    
    public function action_index()
    {
        header('Location: /audio/create');
        die(); 
    }
    
    public function action_create()
    {
        $hash = $this->request->param('hash') ? $this->request->param('hash') : null;
        
        $audio   = Model_Audio::get_by_hash($hash);
        $content = new View('index/audio/create_tpl');
        $content->audio = $audio;
        $this->print_template('Редактирование аудиозаписи', $content);
    }
    
    public function action_remove_noise_reduce_sound()
    {
        $step = 'remove-noise';
        
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax()) {
            $hash  = $this->request->post('hash');
            $begin = $this->request->post('begin');
            $end   = $this->request->post('end');
            $edition = $this->request->post('edition');
            $filename = $this->request->post('filename');
            
            $audio = Model_Audio::get_by_hash($hash);
            if ($audio->loaded()) {
                $audio->set_current_step($step);
                $audio->set_current_edition($edition);
                $audio->set_current_filename($filename);
                if ($filename = $audio->change_volume_of_fragment(0, $begin, $end)) {              
                    $result = array(
                        'success'  => true,
                        'filename' => $filename,
                    );
                }
            }
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();    
    }
    
    public function action_remove_noise_trim_center()
    {
        $step = 'remove-noise';
        
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax()) {
            $hash  = $this->request->post('hash');
            $begin = $this->request->post('begin');
            $end   = $this->request->post('end');
            $edition = $this->request->post('edition');
            $filename = $this->request->post('filename');
            
            $audio = Model_Audio::get_by_hash($hash);
            if ($audio->loaded()) {
                $audio->set_current_step($step);
                $audio->set_current_edition($edition);
                $audio->set_current_filename($filename);
                if ($filename = $audio->trim_center($begin, $end)) {              
                    $result = array(
                        'success'  => true,
                        'filename' => $filename,
                    );
                }
            }
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();    
    }
    
    public function action_remove_noise_trim_ends()
    {
        $step = 'remove-noise';
        
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax()) {
            $hash  = $this->request->post('hash');
            $begin = $this->request->post('begin');
            $end   = $this->request->post('end');
            $edition = $this->request->post('edition');
            $filename = $this->request->post('filename');
            
            $audio = Model_Audio::get_by_hash($hash);
            if ($audio->loaded()) {
                $audio->set_current_step($step);
                $audio->set_current_edition($edition);
                $audio->set_current_filename($filename);
                if ($filename = $audio->trim_ends($begin, $end)) {              
                    $result = array(
                        'success'  => true,
                        'filename' => $filename,
                    );
                }
            }
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();
    }
    
    public function action_first_compression_make_compand()
    {
        $step = 'first-compression';
        
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax()) {
            $hash  = $this->request->post('hash');
            $edition = $this->request->post('edition');
            $filename = $this->request->post('filename');
            
            $audio = Model_Audio::get_by_hash($hash);
            if ($audio->loaded()) {
                $audio->set_current_step($step);
                $audio->set_current_edition($edition);
                $audio->set_current_filename($filename);
                if ($filename = $audio->compand()) {              
                    $result = array(
                        'success'  => true,
                        'filename' => $filename,
                    );
                }
            }
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();
    }
    
    public function action_choose_music_upload()
    {
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax() && HTTP_Request::POST == $this->request->method()) {
            $hash  = $this->request->post('hash');
            
            $audio = Model_Audio::get_by_hash($hash);
            if ($audio->loaded()) {
                if ($filename = $audio->load_background('music')) {
                    $result = array(
                        'success'  => true,
                        'filename' => $filename,
                    );
                }
            }                      
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();
    }
    
    public function action_merge_tracks_cut_background()
    {
        $step = 'merge-tracks';
        
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax() && HTTP_Request::POST == $this->request->method()) {
            $hash  = $this->request->post('hash');
            $begin = $this->request->post('begin');
            $end   = $this->request->post('end');            
            $edition = $this->request->post('edition');
            $filename = $this->request->post('filename');
            
            $audio = Model_Audio::get_by_hash($hash);
            if ($audio->loaded()) {
                $audio->set_current_step($step);
                $audio->set_current_edition($edition);
                $audio->set_current_background_filename($filename);
                if ($filename = $audio->trim_ends($begin, $end, true)) {              
                    $result = array(
                        'success'  => true,
                        'filename' => $filename,
                    );
                }
            }                      
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();    
    }
    
    public function action_merge_tracks_make_fading()
    {
        $step = 'merge-tracks';
        
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax() && HTTP_Request::POST == $this->request->method()) {
            $hash  = $this->request->post('hash');
            $begin = $this->request->post('begin');
            $end   = $this->request->post('end');           
            $edition = $this->request->post('edition');
            $filename = $this->request->post('filename');
            
            $audio = Model_Audio::get_by_hash($hash);
            if ($audio->loaded()) {
                $audio->set_current_step($step);
                $audio->set_current_edition($edition);
                $audio->set_current_filename($filename);
                if ($filename = $audio->fade($begin, $end)) {              
                    $result = array(
                        'success'  => true,
                        'filename' => $filename,
                    );
                }
            }                      
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();        
    }
    
    public function action_merge_tracks_make_merge()
    {
        $step = 'merge-tracks';
        
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax() && HTTP_Request::POST == $this->request->method()) {
            $hash  = $this->request->post('hash');           
            $edition = $this->request->post('edition');
            $filename = $this->request->post('filename');
            $background_filename = $this->request->post('background_filename');
            $track_volume = $this->request->post('track_volume');
            $background_volume = $this->request->post('background_volume');
            
            $audio = Model_Audio::get_by_hash($hash);
            if ($audio->loaded()) {
                $audio->set_current_step($step);
                $audio->set_current_edition($edition);
                $audio->set_current_filename($filename);
                $audio->set_current_background_filename($background_filename);
                if ($filename = $audio->merge($track_volume, $background_volume)) {              
                    $result = array(
                        'success'  => true,
                        'filename' => $filename,
                    );
                }
            }                      
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();    
    }
    
    public function action_second_compression_make_compand()
    {
        $step = 'second-compression';
        
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax()) {
            $hash  = $this->request->post('hash');
            $edition = $this->request->post('edition');
            $filename = $this->request->post('filename');
            
            $audio = Model_Audio::get_by_hash($hash);
            if ($audio->loaded()) {
                $audio->set_current_step($step);
                $audio->set_current_edition($edition);
                $audio->set_current_filename($filename);
                if ($filename = $audio->compand()) {              
                    $result = array(
                        'success'  => true,
                        'filename' => $filename,
                    );
                }
            }
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();
    }
    
    public function action_download_prepare_file()
    {
        $step = 'download';
        
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax()) {
            $hash  = $this->request->post('hash');
            $filename = $this->request->post('filename');
            
            $audio = Model_Audio::get_by_hash($hash);
            if ($audio->loaded()) {
                $audio->set_current_step($step);
                $audio->set_current_filename($filename);
                if ($filename = $audio->prepare_download()) {              
                    $result = array(
                        'success'   => true,
                        'filename'  => $filename,
                    );
                }
            }
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();
    }
    
    public function action_download_file()
    {
        $filename = isset($_GET['filename']) ? $_GET['filename'] : null;
        if ($filename) {
            header('Set-Cookie: fileDownload=true; path=/');
            header('Cache-Control: max-age=60, must-revalidate');
            header('Content-Type: audio/mpeg');
            //header('Content-Type: application/force-download');
            header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\"");
            readfile($filename);
        }
    }
    
} 
